//
//  XSJScrollView.m
//  片刻
//
//  Created by xalo on 15/7/8.
//  Copyright © 2015年 许仕杰. All rights reserved.
//

#import "XSJScrollView.h"

#define KWidth self.frame.size.width
#define KHeight self.frame.size.height
@interface XSJScrollView ()<UIScrollViewDelegate>

/** 滚动视图 */
@property (nonatomic, strong) UIScrollView *scrollView;
/** 左边的相框 */
@property (nonatomic, strong) UIImageView *leftImageView;
/** 中间的相框 */
@property (nonatomic, strong) UIImageView *middleImageView;
/** 右边的相框 */
@property (nonatomic, strong) UIImageView *rightImageView;
/** 图片的数组 */
@property (nonatomic, strong) NSArray *imageArray;
/** 左边图片的下标 */
@property (nonatomic, assign) NSInteger leftIndex;
/** 右边图片的下标 */
@property (nonatomic, assign) NSInteger rightIndex;
/** 小圆点 */
@property (nonatomic, strong) UIPageControl *pageControl;
/** 定时器 */
@property (nonatomic, strong) NSTimer *timer;

@end

@implementation XSJScrollView

#pragma mark --懒加载

//小圆点懒加载
- (UIPageControl *)pageControl{
    if (!_pageControl) {
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(100, KHeight - 30, 175, 30)];
        _pageControl.numberOfPages = self.imageArray.count;
        _pageControl.pageIndicatorTintColor = [UIColor redColor];
        _pageControl.currentPageIndicatorTintColor = [UIColor blueColor];
    }
    return _pageControl;
}

//滚动视图懒加载
- (UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, KWidth, KHeight)];
        _scrollView.contentSize = CGSizeMake(KWidth * self.imageArray.count, KHeight);
        _scrollView.delegate = self;
        _scrollView.pagingEnabled = YES;
        _scrollView.bounces = NO;
        _scrollView.bouncesZoom = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
#warning 初始的scrollView的偏移量
        _scrollView.contentOffset = CGPointMake(KWidth, 0);
    }
    return _scrollView;
}

//左边相框懒加载
- (UIImageView *)leftImageView {
    if (!_leftImageView) {
        
        //创建两个图片
        _leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, KWidth, KHeight)];
        [_leftImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[self.imageArray.count - 1]]];
    }
    return _leftImageView;
}

//右边相框懒加载
- (UIImageView *)rightImageView {
    
    if (!_rightImageView) {
        
        _rightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(KWidth * 2, 0, KWidth, KHeight)];
        [_rightImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[1]]];
    }
    return _rightImageView;
}
//中间相框懒加载
- (UIImageView *)middleImageView {
    if (!_middleImageView) {
        
        _middleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(KWidth , 0, KWidth, KHeight)];
        [_middleImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[0]]];
    }
    
    return _middleImageView;
}

#pragma mark --初始化

- (instancetype)initWithFrame:(CGRect)frame imageArray:(NSArray *)imageArray{
    self = [super initWithFrame:frame];
    if (self) {
        self.rightIndex = 1;
        //给数组赋值
        self.imageArray = imageArray;
        
        self.leftIndex = self.imageArray.count - 1;
        //设置视图
        [self creatScrollView];
    }
    return self;
}

- (void)creatScrollView{
    [self startTimer];
    [self.scrollView addSubview:self.leftImageView];
    [self.scrollView addSubview:self.rightImageView];
    [self.scrollView addSubview:self.middleImageView];
    [self addSubview:self.scrollView];
    [self addSubview:self.pageControl];

}

#pragma mark --timerAction方法

- (void)startTimer{
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    self.timer = timer;
}

- (void)stopTimer{
    [self.timer invalidate];
    self.timer = nil;
}

- (void)timerAction{
    [self.scrollView setContentOffset:CGPointMake(KWidth * 2, 0) animated:YES];
}

#pragma mark --scrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.x == KWidth * 2) {
        scrollView.contentOffset = CGPointMake(KWidth, 0);
        self.pageControl.currentPage = self.rightIndex;
        self.rightIndex++;
        if (self.rightIndex == self.imageArray.count) {
            self.rightIndex = 0;
        }
        self.middleImageView.image = self.rightImageView.image;
        [self.rightImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[self.rightIndex]]];;
        self.leftIndex = self.rightIndex - 2;
        if (self.leftIndex == -1) {
            self.leftIndex = self.imageArray.count - 1;
        }else if (self.leftIndex == -2){
            self.leftIndex = self.imageArray.count - 2;
        }
        [self.leftImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[self.leftIndex]]];;
    } else if (scrollView.contentOffset.x == 0){
        scrollView.contentOffset = CGPointMake(KWidth, 0);
        self.pageControl.currentPage = self.leftIndex;
        self.leftIndex--;
        if (self.leftIndex < 0) {
            self.leftIndex = self.imageArray.count - 1;
        }
        self.middleImageView.image = self.leftImageView.image;
        [self.leftImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[self.leftIndex]]];
        self.rightIndex = self.leftIndex + 2;
        if (self.rightIndex == self.imageArray.count) {
            self.rightIndex = 0;
        }else if (self.rightIndex == self.imageArray.count + 1){
            self.rightIndex = 1;
        }
        [self.rightImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[self.rightIndex]]];
    }

}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self stopTimer];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    [self startTimer];
}







@end
